package com.bailong.administrator.ausedcar.ui.buycarsfragment;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.bailong.administrator.ausedcar.R;
import com.bailong.administrator.ausedcar.bean.BuyCarsBean;
import com.bailong.administrator.ausedcar.ui.detail.DetailActivity;
import com.bailong.administrator.ausedcar.widget.MyGridView;
import com.bailong.administrator.ausedcar.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
//买车碎片
public class BuyCarsFragment extends Fragment implements BuyCarsContract.View {
    @Bind(R.id.fragment_buy_cars_iv)
    ImageView fragmentBuyCarsIv;
    @Bind(R.id.fragment_buy_cars_spinner)
    Spinner fragmentBuyCarsSpinner;
    @Bind(R.id.buycarsfragment_gridview)
    MyGridView buycarsfragmentGridview;
    @Bind(R.id.fragment_buy_cars_spinner_sort)
    Spinner fragmentBuyCarsSpinnerSort;
    @Bind(R.id.fragment_buy_cars_spinner_brand)
    Spinner fragmentBuyCarsSpinnerBrand;
    @Bind(R.id.fragment_buy_cars_image)
    ImageButton fragmentBuyCarsImage;
    @Bind(R.id.buycarsfragment_listview)
    MyListView buycarsfragmentListview;
    private View view;
    private Activity activity;
    private Context mcontext;
    private List<BuyCarsBean.DataBean.ListBean> list = new ArrayList<>();
    private List lists = new ArrayList();
    private BuyCarsAdapter adapter;
    private BuyCarsSingleAdapter singleadapter;
    private boolean flag=true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_buy_cars, container, false);
        BuyCarsPresreter Presreter = new BuyCarsPresreter(this);
        Presreter.getData();
        //绑定黄油刀
        ButterKnife.bind(this, view);
        //下拉列表
        initView();
        return view;
    }

    @Override
    public void onResponse(BuyCarsBean buyCarsBean) {
        setAdapter(buyCarsBean);

    }

    @Override
    public void onFailure(String s) {

    }
    //item点击跳转
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        buycarsfragmentListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mcontext, DetailActivity.class);
                intent.putExtra("carid", list.get(position).getCarid());
                startActivity(intent);
            }
        });

        buycarsfragmentGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mcontext, DetailActivity.class);
                intent.putExtra("carid", list.get(position).getCarid());
                startActivity(intent);
            }
        });
    }



    //加载适配器
    private void setAdapter(final BuyCarsBean buyCarsBean) {


        adapter = new BuyCarsAdapter(list, activity);
        singleadapter = new BuyCarsSingleAdapter(list, activity);
        buycarsfragmentListview.setAdapter(singleadapter);
        buycarsfragmentListview.setEmptyView(buycarsfragmentGridview);
        buycarsfragmentGridview.setAdapter(adapter);
        buycarsfragmentGridview.setEmptyView(buycarsfragmentListview);
        adapter.reloadData(buyCarsBean.getData().getList());
        fragmentBuyCarsImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (flag) {
                    fragmentBuyCarsImage.setBackgroundResource(R.drawable.web);
                    flag = false;
                    adapter.reloadData(lists);

                    singleadapter.reloadData(buyCarsBean.getData().getList());
                } else {

                    fragmentBuyCarsImage.setBackgroundResource(R.drawable.webb);
                    flag = true;

                    singleadapter.reloadData(lists);
                    adapter.reloadData(buyCarsBean.getData().getList());
                }

            }
        });

    }

    //黄油刀解绑
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    //webview
    @OnClick(R.id.fragment_buy_cars_iv)
    public void onClick() {
        Intent intent = new Intent(mcontext, BuyCarsActivity.class);
        startActivity(intent);


    }

    //下拉列表
    public void initView() {
        //添加一个下拉列表项的数组，这里存的项就是下拉列表的菜单项
        String[] spiner = getResources().getStringArray(R.array.buy_cars);
        String[] spinersort = getResources().getStringArray(R.array.buy_cars_sort);
        List<String> spinerbrand = new ArrayList<>();
        spinerbrand.add("全部品牌");
        //为下拉列表定义一个适配器，这里就用到里前面定义的list。
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter(mcontext, android.R.layout.simple_spinner_item, spiner);
        ArrayAdapter<String> sortAdapter = new ArrayAdapter(mcontext, android.R.layout.simple_spinner_item, spinersort);
        ArrayAdapter<String> brandAdapter = new ArrayAdapter(mcontext, android.R.layout.simple_spinner_item, spinerbrand);
        //为适配器设置下拉列表下拉时的菜单样式。
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        brandAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将适配器添加到下拉列表上
        fragmentBuyCarsSpinner.setAdapter(arrayAdapter);
        fragmentBuyCarsSpinnerSort.setAdapter(sortAdapter);
        fragmentBuyCarsSpinnerBrand.setAdapter(brandAdapter);
        //为下拉列表设置各种事件的响应，这个事响应菜单被选中
        fragmentBuyCarsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mcontext, "您选择的是:" + fragmentBuyCarsSpinner.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(mcontext, "您还没有选择:", Toast.LENGTH_SHORT).show();
                parent.setVisibility(View.VISIBLE);
            }
        });
        fragmentBuyCarsSpinnerSort.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mcontext, "您选择的是:" + fragmentBuyCarsSpinnerSort.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(mcontext, "您还没有选择:", Toast.LENGTH_SHORT).show();
                parent.setVisibility(View.VISIBLE);
            }
        });
        fragmentBuyCarsSpinnerBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
